package cn.wolfcode.rbac.web.controller;


import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.SystemDictionaryItem;
import cn.wolfcode.rbac.query.CustomerQueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.ISystemDictionaryItemService;
import cn.wolfcode.rbac.util.JsonResult;
import cn.wolfcode.rbac.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private ICustomerService service;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @RequestMapping("/list")
    @RequiresPermissions(value = {"客户列表", "customer:list"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        PageInfo result = service.query(qo);
        model.addAttribute("result", result);
        List<Employee> emps = employeeService.selectBySn("Market", "Market_Manager");
        model.addAttribute("emps", emps);
        return "customer/list";
    }

    @RequestMapping("/potentialList")
    /*@RequiresPermissions(value = {"潜在客户列表","customer:potentiallist"},logical = Logical.OR)*/
    public String potentiallist(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        Employee employee = UserContext.getCurrentUser();
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("MARKET_MANAGER") || subject.hasRole("admin"))) {
            qo.setSellerId(employee.getId());
        }
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectByParentSn("job");
        model.addAttribute("jobs",jobs);
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectByParentSn("source");
        model.addAttribute("sources",sources);
        List<SystemDictionaryItem> communicationMethod = systemDictionaryItemService.selectByParentSn("communicationMethod");
        model.addAttribute("traceTypes",communicationMethod);
        qo.setStatusId(Customer.STATUS_COMMON);
        PageInfo result = service.query(qo);
        model.addAttribute("result", result);
        List<Employee> emps = employeeService.selectBySn("Market", "Market_Manager");
        model.addAttribute("emps", emps);
        return "customer/potentialList";
    }
    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQueryObject qo) {
        qo.setStatusId(Customer.STATUS_POOL);
        PageInfo result = service.query(qo);
        model.addAttribute("result", result);
        List<Employee> emps = employeeService.selectBySn("Market", "Market_Manager");
        model.addAttribute("emps", emps);
        return "customer/poolList";
    }
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
//    @RequiresPermissions(value = {"客户更新", "customer:saveOrUpdate"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Customer u) {
        try {
            u.setInputuser(UserContext.getCurrentUser());
            service.saveOrUpdate(u);
            return new JsonResult("成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage(), false);
        }
    }
    @RequestMapping("/updateStatus")
    @ResponseBody
//    @RequiresPermissions(value = {"客户更新", "customer:saveOrUpdate"}, logical = Logical.OR)
    public JsonResult updateStatus(Customer u) {
        try {
            service.updateStatus(u);
            return new JsonResult("成功", true);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(e.getMessage(), false);
        }
    }
    @RequestMapping("/test")
    public String test(Customer u) {
      return "test";
    }
}
